import {openCamera,imageOperation} from 'assets/js/toonModule.js'
import Util from 'assets/js/util'
import $ from 'n-zepto';
var doubleSwitch=true;
function doubleClick(){
  if(doubleSwitch){
    doubleSwitch=false;
    setTimeout(function () { doubleSwitch = true }, 2100);
    return true;
  }else{
    return false;
  }
}
export default {
  data(){
    return {
      subjectData: {
        subjectName: "",
        picUrl: "",
        detailContent:"",
        hint: "",
        isShelve: false
      },
      shelveCount: 0,
      showWarnModal:false, //格式校验提示框
      warnContent:''  //提示文案
    }
  },
  mounted(){
    this.init()
  },
  methods:{
    init: function(){
        $(document.body).scrollTop(0);
        this.shelveCount = this.$route.query.shelveCount;
    },
    checkname: function(){
        this.subjectData.subjectName = this.subjectData.subjectName.replace(/[\#]/g,'').replace(/[\＃]/g,'');
    },
    saveAddSubject: function(){
      if(!doubleClick()){
        return;
      };
      var _this = this;
      var exp = /^\s*(.*?)\s*$/;
      var exps = /\s+/;
      this.subjectData.subjectName = this.subjectData.subjectName.replace(exp,"$1").replace(exps,'');
      //this.subjectData.detailContent = this.subjectData.detailContent.replace(exp,"$1").replace(exps,'');
      //this.subjectData.hint = this.subjectData.hint.replace(exp,"$1").replace(exps,'');
      //this.subjectData.detailContent = this.subjectData.detailContent.replace(/[\r\n]{3,}/g,'\r\n\r\n').replace(/[\r\n]*$/g,'').replace(/^[\r\n]*/g,'');
      //this.subjectData.hint = this.subjectData.hint.replace(/[\r\n]{3,}/g,'\r\n\r\n').replace(/[\r\n]*$/g,'').replace(/^[\r\n]*/g,'');
      var params = {
        subjectName: this.subjectData.subjectName,
        picUrl: this.subjectData.picUrl,
        detailContent: this.subjectData.detailContent,
        hint: this.subjectData.hint,
        isShelve: this.subjectData.isShelve==false ? 0 : 1
      };
      if(this.checkEditSubmit()){
          if(params.isShelve==0){
            _this.queryNewSubmit(params);
          }else{
            if(this.shelveCount==5){
              // this.$Modal.warning({
              //   title: '提示',
              //   content: "轮播展示最多有5个专题，请下架后再将此专题上架！"
              // });
              _this.showWarnModal = true;
              _this.warnContent = "轮播展示最多有5个专题，请下架后再将此专题上架！";
            }else{
              _this.queryNewSubmit(params);
            }
          }
      }else{

      }
    },
    checkEditSubmit: function(){
      var i=0,
        errorStr='';
      if(!(this.subjectData.subjectName.length<=12&&this.subjectData.subjectName.length>=1)){
        i++;
        errorStr ="专题名称字数限制为1到12个字!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.subjectName)){
        i++;
        errorStr ="专题名称不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.picUrl==""){
        i++;
        errorStr ="请上传轮播图片!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.detailContent.replace(/[\r\n]/g,'**').length<200){
        i++;
        errorStr ="详情介绍需要二百个以上!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.detailContent)){
        i++;
        errorStr ="详情介绍不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.hint.replace(/[\r\n]/g,'**').length<50){
        i++;
        errorStr ="提示文案需要五十字以上!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.hint)){
        i++;
        errorStr ="提示文案不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(i==0){
        return true;
      }
    },
    outDialog: function(){
      var _this = this;
      openCamera(function (data){
        let params={
          src:'data:image/png;base64,'+data.base64,
          width:379
        };
        console.log("params"+params);

        imageOperation(params,function (yasuohou_base64) {
          var index = yasuohou_base64.indexOf(',');
          let params={
            fileType:'.png',
            fileStream:yasuohou_base64.substring(index+1)
          };
          //请求
          _this.$http.post('convSubjectManage/uploadImg',params)
            .then(function(res){
              console.log("image"+res);
              _this.subjectData.picUrl = res.data.url;
            })
        });
      },null,{},200)

    },
    queryNewSubmit: function(params){
      var _this = this;
      _this.$http.post('convSubjectManage/addSubject',params)
        .then(function(resData){
          if(resData.meta.code == '0'){
            _this.$router.push({
              name: 'subjectList'
            });
          }else{
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: resData.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = resData.meta.message;
          }
        })
        .catch(function(err){
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: "保存失败，请重试！"
          // });
          _this.showWarnModal = true;
          _this.warnContent = "保存失败，请重试！";
        })
    },
    closeModal(){
      this.showWarnModal = false;
    }
  }
}
